package com.hzdl.car.contorlle;

import com.github.pagehelper.PageInfo;
import com.hzdl.car.bean.Role;
import com.hzdl.car.bean.User;
import com.hzdl.car.service.LogService;
import com.hzdl.car.service.RoleService;
import com.hzdl.car.service.UserService;
import com.hzdl.car.util.HzdlResultJson;
import com.hzdl.car.vo.LogVo;
import com.hzdl.car.vo.RoleVo;
import com.hzdl.car.vo.TreeNode;
import com.hzdl.car.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserContorlle {
@Autowired
private UserService service;

@Autowired
private RoleService roleService;

@Autowired
private LogService logService;

    @RequestMapping("loginInit")
    public String loginInit(){
        return "login";
    }


    @RequestMapping("login")
    @ResponseBody//ajax 提交不做页面跳转
    public HzdlResultJson login(UserVo userVo, HttpSession session, HttpServletRequest request){
        User login = service.login(userVo);
        if (login==null){
            return new HzdlResultJson(-1,"您输入的账号或密码不正确") ;
        }else{
            session.setAttribute("session_user",login);
            // 获取当前登录的ip地址
            String ip = request.getRemoteAddr();

            String loginName = login.getLoginname()+"-"+login.getRealname();
            LogVo logVo = new LogVo();
            logVo.setLoginip(ip);
            logVo.setLoginname(loginName);
            logService.add(logVo);
            return  new HzdlResultJson(null);
        }
    }

    @RequestMapping("logOut")
    public String logOut(HttpSession session){
        session.invalidate();
        return "login";
    }

    @RequestMapping("changePwd")
    public String changePwd(){

        return "changePwd";
    }

    @RequestMapping("init")
    @ResponseBody
    public HzdlResultJson Init(UserVo userVo){
        PageInfo<User> pageInfo = service.findAll(userVo);
        return new HzdlResultJson(pageInfo.getTotal(),pageInfo.getList());
    }

    @RequestMapping("add")
    @ResponseBody
    public HzdlResultJson add(UserVo userVo){
        int row = service.add(userVo);
        if (row>0){
            return new HzdlResultJson("添加成功",null);
        }else {
            return new HzdlResultJson(-1,"添加失败");
        }
    }

    @RequestMapping("update")
    @ResponseBody
    public HzdlResultJson update(UserVo userVo){
        int row = service.update(userVo);
        if (row>0){
            return new HzdlResultJson("修改成功",null);
        }
        return new HzdlResultJson(-1,"修改失败");

    }

    @RequestMapping("delete")
    @ResponseBody
    public HzdlResultJson delete ( UserVo userVo){
        int row = service.delete(userVo.getUserid());
        if (row>0){
            return HzdlResultJson.DELETE_SUCCESS();
        }else{
            return HzdlResultJson.DELETE_FAIL();
        }
    }

    @RequestMapping("roleInit")
    @ResponseBody
    public HzdlResultJson roleInit(UserVo userVo){
        return new HzdlResultJson(service.findAll1(userVo));
    }

    @RequestMapping("updateUserRole")
    @ResponseBody
    public HzdlResultJson updateUserRole(UserVo userVo,@RequestParam(value = "ids[]") Integer[] ids){

        int row = service.updateUserRole(userVo,ids);
        if(row>0){
            return HzdlResultJson.DISTRIBUTE_SUCCESS();
        }

        return HzdlResultJson.DISTRIBUTE_FAIL();
    }

    @RequestMapping("deleteAll")
    @ResponseBody
    public HzdlResultJson deleteAll ( UserVo userVo,@RequestParam(value = "ids[]") Integer[] ids){
        int row =0;
        for(Integer userid:ids){
            row+=service.delete(userid);
        }

        if (row>0){
            return HzdlResultJson.DELETE_ALL_SUCCESS();
        }else{
            return HzdlResultJson.DELETE_ALL_FAIL();
        }
    }

    @RequestMapping("resetPwd")
    @ResponseBody
    public HzdlResultJson resetPwd(Integer userid){
        int row = service.resetPwd(userid);
        if (row>0){
            return new  HzdlResultJson("重置成功",null);
        }else{
            return new HzdlResultJson(-1,"重置失败");
        }
    }






}
